Thread: Why multiarray pointer is equal to multiarray[0]?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    Why multiarray pointer is equal to multiarray[0]?

    We create a 2D array with classic declaration:int array[3][4]
    And we create an array with malloc(): marray[3][4]
    In the first case we find that array=array[0]
    BUT in the second marray does not equal to marray[0]
    The second appears to me logical but the first not!
    In the second case we make the marray with this code:

    Code:
    int rows = 10;    
        int cols = 10;
        int *data;
        int **marray;
        int i,j;
    
        /* assume malloc always succeeds */
        data = malloc(rows * cols * sizeof(*data));
        marray = malloc(rows * sizeof(*marray));
        for (i = 0; i < rows; ++i)
        {
            marray[i] = &data[i * cols];
        }
    Last edited by nonlinearly; 11-10-2011 at 02:58 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonlinearly
    In the first case we find that array=array[0]
    No, array and array[0] are not equal. What you probably observed is that array, when converted to a pointer, is equal to &array[0], but that is because an array is converted to a pointer to its first element. Furthermore, you should observe that the value of &array is equal to the value of &array[0], since an array begins at its first element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Quote Originally Posted by laserlight View Post
    No, array and array[0] are not equal.
    I post you a code snippet from the book "Teach Yourself C in 20 Days". Run it...
    Code:
      /* Demonstrates pointers and multidimensional arrays. */
    
      #include <stdio.h>
    
      int multi[2][4];
    
      main()
     {
      printf("\nmulti = %u", multi);
      printf("\nmulti[0] = %u", multi[0]);
      printf("\n&multi[0][0] = %u\n", &multi[0][0]);
      return(0);
      }
    multi = 1328
    multi[0] = 1328
    &multi[0][0] = 1328

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh yeah, I forgot to factor in the fact that you're asking about a 2D array. Anyway, it is still true that multi is not equal to multi[0], since their types differ in an incompatible way. After having read my explanation in post #2, why do you think that the value of multi and the value of multi[0], when they are converted to pointers, are the same?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Ok redefines the question...
    We know that when we have a 2D array for example array[2][3] then array is a pointer that points to a second pointer a[0] ie array=&array[0]. And this second pointer points to the address of the a[0][0] element ie &array[0][0]. It seems that this is true only when we set the table with dynamic allocated memory. In case we have a not dynamic declared array then the first pointer (array) points to the same address that array[0] points ie &array[0][0]. This is evident in the previous code.
    Last edited by nonlinearly; 11-10-2011 at 03:38 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonlinearly
    We know that when we have a 2D array for example array[2][3] then array is a pointer that points to a second pointer a[0].
    No, an array is not a pointer. If we have a 2D array, then when the array is converted to a pointer to its first element, it is converted to a pointer to an array, not a pointer to a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    1. Ok if array is not a pointer then what is it?
    To convert something means that it was something before.
    2. when you say "If we have a 2D array" with witch method the array created? With malloc or not?
    3. In your code you have declared the array as pointer to pointer!!! Thus it is pointer to pointer.
    4. and finally if you say that array is not equal to array[0] then why the snippet code from the book refutes you?

    Thanks

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    I say again and no one can refute this (try it):
    if we have an array a[2][3] created with the malloc() like the code in the fist message then array does not equal to array[0] (it seems logical)
    if we have an array declared as: int array[2][3] then array=array[0]
    Why?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonlinearly
    1. Ok if array is not a pointer then what is it?
    An array

    Quote Originally Posted by nonlinearly
    2. when you say "If we have a 2D array" with witch method the array created? With malloc or not?
    Without malloc, i.e., just a normal fixed size array. malloc just allocates memory space and returns a pointer through which we can access the memory; how we interpret the memory allocated is up to us. In this sense, we do not actually create objects of any given type with malloc; rather, we allocate space for the object(s), then access the space through a pointer of the appropriate type such that it appears that we have created the object(s), and in effect, we have created them.

    Quote Originally Posted by nonlinearly
    3. In your code you have declared the array as pointer to pointer!!! Thus it is pointer to pointer.
    No, in my code, I declared a pointer to a pointer. It is not an array. However, the idea is to use this pointer to a pointer as if it were an array of arrays, since you desired to use the array index notation.

    Quote Originally Posted by nonlinearly
    4. and finally if you say that array is not equal to array[0] then why the snippet code from the book refutes you?
    The snippet from your book does not demonstrate that array is equal to array[0]. It demonstrates that the value of array is equal to the value of array[0], when array and array[0] are converted to pointers to their respective first elements. Yes, I know this distinction sounds very pedantic, but when we are discussing this topic of arrays versus pointers, it is necessary to be technically correct concerning arrays versus pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonlinearly
    if we have an array a[2][3] created with the malloc() like the code in the fist message then array does not equal to array[0] (it seems logical)
    Yes, it is logical because a pointer and what the pointer points to are two different things that exist in different locations in memory.

    Quote Originally Posted by nonlinearly
    if we have an array declared as: int array[2][3] then array=array[0]
    Compile this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int array[2][3] = {{0}};
        if (array == array[0]) {
            puts("equal in value");
        }
        return 0;
    }
    Your compiler should emit a warning. If not, compile with a higher warning level.

    Anyway, I have already explained: an array is converted to a pointer to its first element, hence array and array[0] have the same value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Ok I have understand...
    To conclude therefore:
    the way we simulate an array with the malloc has nothing to do with how C perceives a real table (least as regards the interpretation of "array" -the first synthetic-).
    So we must be careful when we have a reference to "array" (only) somewhere in the code.
    We should seek how created. Because:
    1. in the malloc() case array is a pointer that points to a pointer that this second pointer points to the first element of a series of items (deliberately did not mention the word array) and WE give the interpretation that it is a table
    2. in the case of a normal fixed size TRUE array is a pointer to the array as a whole and because the start position of the array is the &a[0][0] then the value of array=&a[0][0].

    (although I did not understand that C considers on the one hand an array as a whole and on the other hand break it in a separate sub-arrays a[0], a[1],...)

    Thank you
    Last edited by nonlinearly; 11-10-2011 at 05:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number of elements in multiarray
    By AcId9381 in forum C Programming
    Replies: 4
    Last Post: 12-10-2010, 12:54 PM
  2. -1 is equal to -127?
    By nkrao123@gmail. in forum C Programming
    Replies: 9
    Last Post: 11-26-2009, 05:17 AM
  3. boost MultiArray
    By anon in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2009, 04:19 PM
  4. Replies: 3
    Last Post: 01-25-2006, 10:30 PM
  5. does not equal
    By Klinerr1 in forum C++ Programming
    Replies: 18
    Last Post: 07-29-2002, 10:38 AM